module.exports = {
  basePath: 'https://user0.sungivenplus.com/api/', //新测试环境
  storePath: 'https://store0.sungivenplus.com/api/',
  // basePath: 'https://user.sungivenplus.com/api/', //生产环境
  // storePath: 'https://store.sungivenplus.com/api/',
  gdKey: '553750cc24e9d426636f453ea3547e95',
  serverTel: '400-096-8066',
  showFail: function (msg) {
    wx.showToast({
      image: '/imgs/icon/fail.png',
      title: msg
    })
  },
  showLoading: function () {
    wx.showLoading({
      title: '',
    })
  },
  getAddress: function () {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + 'address/list/', "", res => {
        if (res.data.errno == "0") {
          resolve(res.data)
        } else {
          if (res.data.errno != '100012') {
            that.showErrorToast(res.data.errmsg);
          }
        }
      })
    })
  },
  getPlaceAround: function (longitude, latitude) {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + 'address/getplacearound/', {
        "longitude": longitude,
        "latitude": latitude,
        "city": getApp().globalData.locateCity
      }, res => {
        if (res.data.errno == "0") {
          resolve(res.data)
        } else {
          that.showErrorToast(res.data.errmsg);

        }
      })
    })
  },
  searchPlace: function (keyword, city) {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + 'address/getplacepoi', {
        'keyword': keyword,
        "city": city
      }, res => {
        if (res.data.errno == "0") {
          resolve(res.data);
        } else {
          that.showErrorToast(res.data.errmsg);

        }
      })
    })
  },
  getStoreByLaLon(lon, la, returntype, city, storetype, keyword) {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + "store/search/", {
        "longitude": lon,
        "latitude": la,
        "returntype": returntype,
        "city": city,
        "keyword": keyword
      }, res => {
        that.hideLoading();
        wx.setStorage({
          key: 'isDeliverable',
          data: 0
        })
        resolve(res.data)
      })
    })
  },
  //获取订单详情
  getOrdeDetail(orderId) {
    var server = require('server');
    var that = this;
    this.showLoading();
    that.hideLoading();
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + "order/detail/", {
        "order_id": orderId
      }, res => {
        if (res.data.errno == "0") {
          resolve(res.data)
        } else {
          that.showErrorToast(res.data.errmsg);
        }
      })
    })
  },
  //再来一单
  getOrderAgain(param) {
    var server = require('server');
    var that = this;
    this.showLoading();
    that.hideLoading();
    if (param.store_id == "") {
      getApp().globalData.skipType = 1;
      wx.navigateTo({
        url: '../../pages/shop/productsListIndex',
      })
    } else {
      return new Promise(function (resolve, reject) {
        server.postJSON(that.storePath + "order/again/", param, res => {
          if (res.data.errno == "0") {
            getApp().globalData.cartIndexType = 2;
            wx.switchTab({
              url: '../../pages/cart/cartIndex',
            })
            resolve(res.data)
          } else {
            that.showErrorToast(res.data.errmsg)
          }
        })
      })
    }
  },
  // 获取退款详情
  getRefundDetail(orderId) {
    var server = require('server');
    var that = this;
    this.showLoading();
    this.hideLoading();
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + "refund/detail/", {
        "order_id": orderId
      }, res => {
        if (res.data.errno == "0") {
          that.hideLoading();
          resolve(res.data)
        } else {
          that.showErrorToast(res.data.errmsg)
        }
      })
    })
  },
  pay(param) {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + "order/pay/", param, res => {
        resolve(res.data)
      })
    })
  },
  getCityList() {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      server.postJSON(that.storePath + "store/getcity/", '', res => {
        resolve(res.data)
      })
    })
  },
  compareVersion(v1, v2) {
    console.log(v1)
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }

    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])

      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }

    return 0
  },
  hideLoading: function () {
    wx.hideLoading()
  },
  showToast: function (mesg) {
    wx.showToast({
      title: mesg,
      icon: 'none'
    })
  },
  showErrorToast: function (mesg) {
    if (mesg == undefined) {
      this.showToast("数据出错啦T_T，请稍候再试 [-3]");
    }
    wx.showToast({
      title: mesg,
      icon: 'none'
    })
  },
  call: function (e) {
    var tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  // 校验登录
  checkLogin: function () {
    var server = require('server');
    var that = this;
    return new Promise(function (resolve, reject) {
      wx.login({
        success: resp => {
          wx.getUserInfo({
            success: res => {
              var code = resp.code,
                userParams = {
                  "js_code": code,
                  "encryptedData": res.encryptedData,
                  "iv": res.iv,
                  "userinfo": res.userInfo,
                  "longitude": getApp().globalData.locateLongitude,
                  "latitude": getApp().globalData.locateLatitude,
                  "province": getApp().globalData.locateProvince,
                  "city": getApp().globalData.locateCity
                };
              server.login(that.basePath + 'miniprogram/login/', userParams, res => {
                resolve(res.data);
              })
            },
            fail: res => {
              console.log(res)
              resolve(res);
            }
          })
        }
      })
    })
  }
}